
<template>
  <div class="view-container">
    <div class="app-title-container title-position">
      <modules-title
        title-name="立项审批"
        style="margin-bottom: 20px"
      >
        <template slot="btn">
          <!-- <el-button
            v-hasPermi="{
              v: ['project:my:submitProjectApproval'],
              isF: true,
            }"
            style="margin-left: 20px"
            type="primary"
            size="mini"
            @click="createSelfProjectAudit"
          >创建自研项目立项审批
          </el-button>
          <el-button
            v-hasPermi="{
              v: ['project:my:submitProjectApproval'],
              isF: true,
            }"
            style="margin-left: 20px"
            type="primary"
            size="mini"
            @click="createProjectAudit"
          >创建委托项目立项审批
          </el-button> -->
          <el-button
            v-hasPermi="{
              v: ['project:shenpi:add'],
              isF: true,
            }"
            style="margin-left: 20px"
            type="primary"
            @click="createAudit"
          >创建立项审批
          </el-button>
        </template>
      </modules-title>
    </div>
    <div class="main-content-container">
      <el-form
        v-show="showSearch"
        ref="queryForm"
        class="filter-form"
        :model="queryParams"
        :inline="true"
        label-width="68px"
      >
        <!-- <el-form-item label="" style="margin-right: 40px">
          <el-select
            v-model="queryParams.select_type"
            placeholder="请选择"
            size="small"
            @change="filterChange"
          >
            <el-option
              v-for="fr in categoryOption"
              :key="fr.id"
              :label="fr.label"
              :value="fr.value"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item
          label="立项状态"
          style="margin-right: 40px"
        >
          <el-select
            v-model="queryParams.check_status"
            placeholder="请选择状态"
            size="small"
            @change="filterChange"
          >
            <el-option
              v-for="(dict,dinx) in statusOptions"
              :key="dict.id+dinx"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="项目类型"
          style="margin-right: 40px"
        >
          <el-select
            v-model="queryParams.project_type_id"
            placeholder="请选择项目类型"
            clearable
            :style="{ width: '100%' }"
            @change="filterChange"
          >
            <el-option
              v-for="(item, index) in projectTypeOptions"
              :key="index"
              :label="item.dict_val"
              :value="item.id"
              :disabled="item.disabled"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="申报类别" style="margin-right: 40px">
          <el-select
            v-model="queryParams.apply_type_id"
            placeholder="请选择注册分类"
            clearable
            :style="{width: '100%'}"
            @change="filterChange"
          >
            <el-option
              v-for="(item, index) in applyTypeOptions"
              :key="index"
              :label="item.dict_val"
              :value="item.id"
              :disabled="item.disabled"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item
          label="申请时间"
          style="margin-right: 40px"
        >
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="dateRangeChang"
          />
        </el-form-item>
        <el-form-item
          label="关键字"
          style="margin-right: 40px"
        >
          <el-input
            v-model="queryParams.select_str"
            clearable
            placeholder="请输入项目编号或项目名称"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="small"
            @click="handleSearch"
          >搜索
          </el-button>
          <el-button
            plain
            size="small"
            @click="resetQuery"
          >重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        border
        size="medium"
        :data="tableList"
        :cell-style="tableCellStyle"
        row-key="id"
        @row-click="tableRowClick"
      >
        <el-table-column
          label="审批编号"
          align="center"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.info_check?scope.row.info_check.SerialNumber:'' }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="项目编号"
          align="center"
          prop="project_no"
          width="100"
        />
        <el-table-column
          label="申请项目名称"
          align="left"
          header-align="center"
          prop="project_name"
          width="240px"
        />
        <el-table-column
          label="申请项目来源"
          align="center"
          prop="project_source"
        />
        <el-table-column
          label="申请项目类型"
          align="center"
          prop="project_type"
        />
        <el-table-column
          label="申报类别"
          align="center"
          prop="apply_type"
        />
        <el-table-column
          label="药物类别"
          align="center"
          prop="medical_type"
        />
        <el-table-column
          label="申请人"
          align="center"
          prop="creator_name"
        />
        <el-table-column
          label="申请时间"
          align="center"
          prop="created_at"
          width="160"
        />
        <el-table-column
          label="状态"
          align="center"
          prop="info_status"
        >
          <template slot-scope="scope">
            <div>
              <p
                v-if="scope.row.info_status == 7"
                class="status-common"
              >
                审批中
              </p>
              <p
                v-else-if="scope.row.info_status == 8"
                class="status-common"
              >
                拒绝
              </p>
              <p
                v-else-if="scope.row.info_status == 9"
                class="status-common"
              >
                通过
              </p>
              <p
                v-else-if="scope.row.info_status == 11"
                class="status-common"
              >
                审批中[已回退]
              </p>
              <p
                v-else-if="scope.row.info_status == 12"
                class="status-common"
              >
                已撤销
              </p>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="left"
          header-align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <!-- <el-button
              v-hasPermi="{v:['project:my:projectApproval','project:shenpi:approval'],isF: scope.row.info_status==7}"
              size="medium"
              type="text"
              @click="projectApprovalOperation(scope.row)"
            >
              审批</el-button> -->
            <el-button
              v-if="scope.row.info_status == 9&&scope.row.creator_id===userInfo.user.id"
              size="medium"
              type="text"
              @click="printApproval(scope.row)"
            >
              打印立项审批表</el-button>
            <template v-else>
              <el-button
                v-hasPermi="{
                  v: ['project:shenpi:printApproval'],
                  isF: scope.row.info_status == 9,
                }"
                size="medium"
                type="text"
                @click="printApproval(scope.row)"
              >
                打印立项审批表</el-button>
            </template>
            <el-button
              v-if="scope.row.info_status == 11 && scope.row.info_check&&scope.row.info_check.check_creator_id===userInfo.user.id"
              size="medium"
              type="text"
              @click="submitProjectApproval(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              v-if="scope.row.info_check&&scope.row.info_check.Cancelable==true&&scope.row.info_check.check_creator_id===userInfo.user.id"
              size="medium"
              type="text"
              @click="cancelOperation(scope.row.info_check.id)"
            >撤销</el-button>
            <!-- <el-button
              v-hasPermi="{ v: ['project:shenpi:detail'] }"
              class="text-btn-yellow"
              size="medium"
              type="text"
              @click="details(scope.row)"
            >
              详情</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <pagination
        style="display: flex; justify-content: flex-end"
        :total="total"
        :page.sync="queryParams.pager.skip"
        :limit.sync="queryParams.pager.limit"
        @pagination="getList"
      />
    </div>
    <!-- 已经通过的自研立项dialog -->
    <PassProjectListDialog ref="passProjectListDialog" />
    <!-- 立项审批 -->
    <project-approval-dialog
      ref="projectApprovalDialog"
      :project-id="currentId"
      @projectApprovalSuccess="projectApprovalSuccess"
    />
    <!-- 打印立项审批表 -->
    <vue-easy-print
      v-if="detailData"
      ref="print"
      table-show
    >
      <!-- 你自己编写打印的模板  -->
      <template slot-scope="func">
        <div id="print-template">
          <approval-print
            :detail-data="detailData"
            :get-chinese-number="func.getChineseNumber"
          />
        </div>
      </template>
    </vue-easy-print>
  </div>
</template>
<script>
import {
  approveList,
  auditDetail,
  approvalOpinion
} from '@/api/projectInformation'
export default {
  name: 'ProjectInfoManagement',
  components: {
    ProjectApprovalDialog: () =>
      import('../../modules/projectApprovalDialog.vue'),
    ApprovalPrint: () => import('../../modules/approvalPrint.vue'),
    PassProjectListDialog: () => import('../../modules/passProjectListDialog')
  },
  data() {
    return {
      userInfo: null,
      // 当前id
      currentId: '',
      detailData: null,
      showSearch: true,
      // 总条数
      total: 0,
      // 流程审批测试表格数据
      tableList: [],
      statusOptions: [
        { id: 1, value: -1, label: '全部' },
        { id: 2, value: 1, label: '审批中' },
        { id: 3, value: 2, label: '同意' },
        { id: 4, value: 3, label: '拒绝' },
        { id: 5, value: 4, label: '审批中[已回退]' },
        { id: 6, value: 5, label: '已撤销' }
      ],
      categoryOption: [
        { id: 1, value: 0, label: '全部' },
        { id: 2, value: 1, label: '我发起的' },
        { id: 3, value: 2, label: '我处理的' }
      ],
      projectTypeOptions: [],
      applyTypeOptions: [],
      dateRange: '',
      // 查询参数
      queryParams: {
        pager: {
          limit: 10,
          skip: 1
        },
        check_status: -1,
        select_type: 0,
        project_type_id: '',
        apply_type_id: '',
        select_str: '',
        start_time: '',
        end_time: ''
      }
    }
  },
  // 返回后是否刷新列表
  beforeRouteEnter(to, from, next) {
    console.log(to, from, next)
    to.params.refresh = from.params.refresh

    next()
  },
  activated: function() {
    if (this.$route.params.refresh) {
      this.getList()
    }
  },
  created() {
    this.userInfo = this.$store.getters.userInfo
    this.getSysAllDic()
    this.getList()
  },
  methods: {
    // 创建立项审批
    createAudit() {
      this.$router.push({
        name: 'submitApproval',
        query: {
          types: 2
        }
      })
    },
    // 创建自研立项审批
    createSelfProjectAudit() {
      this.$refs.passProjectListDialog.open()
    },
    // 创建立项审批
    createProjectAudit() {
      // 直接创建立项审批
      this.$router.push({
        name: 'submitApproval',
        query: {
          types: 2
        }
      })
    },
    getSysAllDic() {
      let allDic = this.$store.getters.sysDic()
      if (!allDic) {
        this.$store.dispatch('sys/getSysAllDic').then((data) => {
          allDic = data
        })
      }
      this.applyTypeOptions = allDic['apply_type']
      this.projectTypeOptions = allDic['project_type']
    },
    dateRangeChang(val) {
      console.log(val)
      if (val && val.length) {
        this.queryParams.start_time = val[0]
        this.queryParams.end_time = val[1]
      } else {
        this.queryParams.start_time = ''
        this.queryParams.end_time = ''
      }
      this.getList()
    },
    filterChange() {
      this.queryParams.pager.skip = 1
      this.getList()
    },
    async printApproval(row) {
      let datas = {}
      const res = await auditDetail({ id: row.id })
      const remark = await approvalOpinion({ id: row.info_check.id })
      if (res.code === 200) {
        datas = res.data || {}
      }
      if (remark.code === 200) {
        datas = { ...datas, ...remark.data }
      }
      this.detailData = datas
      console.log(this.detailData)
      setTimeout(() => {
        this.$refs.print.print()
      }, 50)
    },
    /** 查询流程审批测试列表 */
    getList() {
      approveList(this.queryParams).then((response) => {
        this.tableList = response.data.data
        this.total = response.data.total
      })
    },
    tableRowClick(row, column, event) {
      if (column.label === '操作') return
      this.$router.push({
        name: 'auditDetail',
        query: {
          projectId: row.id,
          demandInfoId: row.demand_info_id
        }
      })
    },
    tableCellStyle(row) {
      if (row.column.label === '操作') {
        return
      } else {
        return 'cursor: pointer'
      }
    },
    details(row) {
      this.$router.push({
        name: 'auditDetail',
        query: {
          projectId: row.id,
          demandInfoId: row.demand_info_id
        }
      })
    },
    // 修改立项审批
    submitProjectApproval(row) {
      this.$router.push({
        name: 'submitApproval',
        query: {
          auditProjectId: row.id,
          types: 1
        }
      })
    },
    // 表单重置
    reset() {
      this.dateRange = ''
      this.queryParams = {
        pager: {
          skip: 1,
          limit: 10
        },
        check_status: -1,
        select_type: 0,
        project_type_id: '',
        apply_type_id: '',
        select_str: '',
        start_time: '',
        end_time: ''
      }
    },
    /** 搜索按钮操作 */
    handleSearch() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.reset()
      this.handleSearch()
    },
    /** 审批按钮操作 */
    // 立项审批操作
    projectApprovalOperation(row) {
      this.currentId = row.id
      this.$refs.projectApprovalDialog.open()
    },
    projectApprovalSuccess() {
      this.currentId = ''
      this.getList()
    },
    // 撤销
    cancelOperation(id) {
      this.$alert('是否确认撤销？', '撤销', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        const subdata = {
          flow_id: id,
          model_type: 'BuildProject'
        }
        this.$comFun
          .cancelFunc(subdata)
          .then(() => {
            this.$message({
              type: 'success',
              message: '已撤销'
            })
            this.getList()
          })
          .catch((err) => {
            this.$message.error(err)
          })
      }).catch(err => {
        console.log(err)
      })
    }
  } // methods结束
}
</script>

<style lang="scss" scoped>
.view-container {
  padding-top: 110px;
  box-sizing: border-box;
  .filter-form {
    ::v-deep .el-input {
      width: 160px;
    }
  }
}
</style>
